#include <menu.h>  // This also includes the curses.h file
#include <stdlib.h>
#include <stdio.h>
#include <aio.h>
#include <sys/time.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include "./otherCars.h"
#include "./highscore_easyA.h"

struct aiocb kbcbuf;

int strow;
int stcol;
int quit = 0;
int sec = 0;	// Time starts at 0

WINDOW *timer_win;

// prints updated time to timer window
int disp_timer (int sec )
{
        if(sec == 30)
        {
                wclear(timer_win);
                return 1;
        }
        mvwprintw(timer_win, 1, 3, "Time elapsed:");
        mvwprintw(timer_win, 2, 3, "%02i seconds", sec);
        wrefresh(timer_win);
        return 0;
}

// Counts down to zero
void alarm_handler ( int sig )
{
        sec++;
        if(sec == 30)
        {
                mvprintw(getmaxy(stdscr)/2, getmaxx(stdscr)/2, "Game Over");
                refresh();
        }
        mvwprintw(timer_win, 1, 3, "Time elapsed:");
        mvwprintw(timer_win, 2, 3, "%02i seconds", sec);
        wrefresh(timer_win);
}

// Ending instructions
void finish(int sig)
{
  endwin();
  exit(0);
}

int main(){

        void    on_input();
        void    setup_aio_buffer();
        struct itimerval value;

        int             row, 
                        col,
                    carRow=0,       //carRow and laneRow store the rows the functions last stoped printing on
            laneRow=0,
			end = 0;
        
        initscr();                      // Start curses mode
        crmode();                       // Line buffering disabled
        noecho();
        clear();

        getmaxyx(stdscr,row,col);

        timer_win = newwin(row, 22, 0, col - 22);	// Status bar
        
        strow = row - 3;
        stcol = (col/4.0) +9;
        
        signal(SIGIO, on_input);
        setup_aio_buffer();
        aio_read(&kbcbuf);

        signal(SIGALRM, alarm_handler);                 // Sets timer to update
        memset(&value, 0, sizeof(value));               // every second
        value.it_interval.tv_sec = 1;
        value.it_value.tv_sec    = 1;
        setitimer(ITIMER_REAL, &value, NULL); /* alarm reset after timer countdown */

        signal(SIGINT, finish); /* ^C exits and cleans up program */

while(!quit && !end)
        {
                laneRow = drawLanesEasy(SPEED_LV1, stdscr, laneRow);               //call draw lanes with speed constant, window pointer, and the row it was last on
                carRow = other_cars(SPEED_LV1 , stdscr, carRow);   //same as above but prints the "other" cars
                carRow+=1;      //move car one space down
                laneRow+=3;     //move lanes down by 3 to allow spaces in the lines
        
        
                        mvprintw(strow-1, stcol-8, "      ");
                                mvprintw(strow, stcol-8, "      ");
                                mvprintw(strow+1, stcol-8, "      ");
                                
                                mvprintw(strow-1, stcol+8, "      ");
                                mvprintw(strow, stcol+8, "      ");
                                mvprintw(strow+1, stcol+8, "      ");
                                
                        mvprintw(strow-1, stcol, "(|  |)");
                                mvprintw(strow, stcol, " |  |");
                                mvprintw(strow+1, stcol, "(|  |)");

			// Keeps timer updating.
			box(timer_win, 0, 0);
                        end = disp_timer(sec);
			box(timer_win, 0, 0);
                        mvwprintw(timer_win, 5, 3, "Difficulty: EASY");
                        mvwprintw(timer_win, 8, 3, "Level: 1");
                        mvwprintw(timer_win, 15, 3, "q to quit");
                        wrefresh(timer_win);

	}

        setitimer(ITIMER_REAL, NULL, NULL); /* Turns off timer alarm */

	echo();

	if(end == 0)			// If user pushed 'q', enter
		highscore_easy(sec);	// high score
	else				// Otherwise, game over.
	{
		wclear(timer_win);
		clear();
		mvprintw(col/2, (row/2) - 6, "Game Over...");
		refresh();
		sleep(5);
	}
	noecho();

        endwin();
        return 0;
        
        
}
        
        
void on_input()
{
        int  c, row, col;
        char *cp = (char *) kbcbuf.aio_buf;     /* cast to char * */

                getmaxyx(stdscr,row,col);
                
        /* check for errors */
        if ( aio_error(&kbcbuf) != 0 )
                perror("reading failed");
        else
                /* get number of chars read */
                if ( aio_return(&kbcbuf) == 1 )
                {
                      c = *cp;
                      
                      if((c == 'a') && (stcol > ((col/4.0)+8))) stcol-=8;
                      if((c == 'd') && (stcol < ((col/4.0)+16))) stcol+=8;
                      if(c == 'q') quit=1;
                }

        /* place a new request */
        aio_read(&kbcbuf);
}

void setup_aio_buffer()
{
        static char input[1];                 /* 1 char of input */

        /* describe what to read */
        kbcbuf.aio_fildes     = 0;            /* standard intput */
        kbcbuf.aio_buf        = input;        /* buffer          */
        kbcbuf.aio_nbytes     = 1;             /* number to read  */
        kbcbuf.aio_offset     = 0;             /* offset in file  */

        /* describe what to do when read is ready */
        kbcbuf.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
        kbcbuf.aio_sigevent.sigev_signo  = SIGIO;  /* send sIGIO   */
}

